import {createRouter, createWebHistory} from 'vue-router'
import {staticRoutes} from "@/router/staticRoutes"

const routes = staticRoutes;

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

router.beforeEach((to, from, next) => {
    if (to.path === '/login' || to.path === '/register') {
        next()
        return
    }
    let user = sessionStorage.getItem("user") ? JSON.parse(sessionStorage.getItem("user")) : {}
    if (!user.permissions || !user.permissions.length) {
        next('/login')
    } else if (!user.permissions.find(p => p.path === to.path)) {
        next('/login')
    } else {
        next()
    }
})

export default router
